//
//  GravityAgent.m
//  GravityControl
//
//  Created by Javier Quevedo on 10/06/2009.
//  Copyright 2009 VRIJE. All rights reserved.
//

#import "GravityAgent.h"

#define kAccelerometerFrequency     10
#define Pi 3.14159

@implementation GravityAgent
@synthesize xThreshold, yThreshold, zThreshold, velocity, accel, angle;


static GravityAgent *sharedGravityAgent = nil;

+ (GravityAgent*)sharedAgent
{
    @synchronized(self) {
        if (sharedGravityAgent == nil) {
            [[self alloc] init]; // assignment not done here

        }
    }
    return sharedGravityAgent;
}

+ (id)allocWithZone:(NSZone *)zone
{
    @synchronized(self) {
        if (sharedGravityAgent == nil) {
            sharedGravityAgent = [super allocWithZone:zone];
			[[UIAccelerometer sharedAccelerometer] setDelegate:sharedGravityAgent];
			[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
			
		
            return sharedGravityAgent;  // assignment and return on first allocation
        }
    }
    return nil; //on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain
{
    return self;
}

- (unsigned)retainCount
{
    return UINT_MAX;  //denotes an object that cannot be released
}


- (void)release
{
    //do nothing
}

- (id)autorelease
{
    return self;
}



#pragma mark Accelerometer delegates
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
   // NSLog(@"x: %f, y: %f", acceleration.x, acceleration.y);
	[self setAccel:acceleration];

	[[NSNotificationCenter defaultCenter] postNotificationName:nGravityUpdated object:self];
	
}

@end
